<template lang="jade">

div.return-bar
	i.iconfont(@click="back()") &#xe668;
	{{ title_msg }}
div.user-auto.detail_top(v-if=" typeId == 2")
	dl.com.space
		dt 请选择月份
		dd(v-bind:class="{'activated':period=='0'}", v-on:click="pickup('p', 0)")

			span.title 不限
			span.content
				i.iconfont.yes &#xe62d;


		dd(v-bind:class="{'activated':getPeriod('5')}", v-on:click="pickup('p', 5)")
			span.title 1至3个月
			span.content
				i.iconfont.yes &#xe62d;


		dd(v-bind:class="{'activated':getPeriod('1')}", v-on:click="pickup('p', 1)")
			span.title 4至6个月
			span.content
				i.iconfont.yes &#xe62d;

		dd(v-bind:class="{'activated':getPeriod('2')}", v-on:click="pickup('p', 2)")
			span.title 7至12个月
			span.content
				i.iconfont.yes &#xe62d;

		dd(v-bind:class="{'activated':getPeriod('3')}", v-on:click="pickup('p', 3)")
			span.title 13至24个月
			span.content
				i.iconfont.yes &#xe62d;

		dd(v-bind:class="{'activated':getPeriod('4')}", v-on:click="pickup('p', 4)")
			span.title 25至36个月
			span.content
				i.iconfont.yes &#xe62d;

	button.obtn(@click="submitMoney(1)") 确认


div.user-auto.detail_top(v-if=" typeId == 3")
	dl.com.space
		dt 选择期限
		dd.noline(v-bind:class="{'activated':money=='0'}", v-on:click="pickup('m', 0)")

			span.title 不限
			span.content
				i.iconfont.yes &#xe62d;

		dt 入输金额
		dd(v-bind:class="{'activated':cview == '1' || money=='1'}", v-on:click="pickup('m', 1)")
			span.title 自定
			span.content
				i.iconfont.yes &#xe62d;
		dd
			input( v-model="min_money", type="text", placeholder="输入单笔最低金额(≥100元)", v-on:click="pickup('m', 1), min_money='' " )
		dd
			input(v-model="max_money", type="text", placeholder="输入单笔最高金额(为100元倍数)", v-on:click="pickup('m', 1), max_money='' " )
	button.obtn(@click="submitMoney(2)") 确认



div.user-auto.detail_top(v-if=" typeId == 4")
	dl.sub.space
		dt 选择项目
		dd(v-bind:class="{'activated': borrow_type=='0'}", v-on:click="pickup('t', 0)")
			span.title 不限
			span.content
				i.iconfont.yes &#xe62d;
		dd(v-bind:class="{'activated':cview == '1' || borrow_type=='1'}", v-on:click="pickup('t', 1)")
			span.title 房贷
			span.content
				i.iconfont.yes &#xe62d;
		dd(v-bind:class="{'activated':cview == '2' || borrow_type=='2'}", v-on:click="pickup('t', 2)")
			span.title 车贷
			span.content
				i.iconfont.yes &#xe62d;



div.user-auto.detail_top(v-if=" typeId == 5")
	dl.com.space
		dt 保留金额
		dd
			input( v-model="retain_min_money", type="text", placeholder="输入保留金额" ,v-on:click=" retain_min_money='' ")

	button.obtn(@click="submitMoney(5)") 确认


div.homeloading
	loading(:show="show_loading", text="正在加载")
</template>

<script>
import '../../assets/sass/user/auto-buy.scss'
import {Api_Set_AutoBuy, Api_Get_AutoBuy} from '../../config/api'
import {Loading} from 'vux'

export default{
	data(){
		return {
			cview:'0',
			typeId:'2',
			i:1,
			min_money:'',
			max_money:'',
			show_loading:false,
			retain_min_money:'',
			busy:false,
			min_period:'',
			max_period:'',
			period:[],
			//rperiod:[],
			money:'',
			borrow_type:'0',
			data:{},
			title_msg:''
		}
	},

	components: {
		Loading
	},

	methods:{
		editPeriod(key){
			var rval = '', add = true, j = 0;


			//'1,2,3'
			//'1|3|4'
			for(var i in this.period){
				if(this.period[i] == key){
					add = false, j = i;
				}
			}
			if(add)
				this.period.push(key)
			else
				this.period.splice(j, 1);

		},

		getPeriod(key){
			var rval=false;
			for(var i in this.period){
				if(this.period[i]==key)
					rval = true;
			}
			return rval;
		},

		pickup(p, key){

			var that = this;
			this.cview = key;

			switch(p){
				case "p":
					if(key == 0)
						that.period = ['0'];
					else{
						if(this.period.length==1 && this.period[0] == 0)
							that.period = [];
						that.editPeriod(key);
					}
					break;

				case "m":
					that.money = key;
					break;

				case "t":
					that.borrow_type = key;
					break;
			}
			//console.log(this.period.join('|'))
		},

		back(){
			this.$route.router.go('/auto-buy')
		},
		valid(str){
			if(str == "") return false;
			if(str == null) return false;
			if(str == undefined) return false;
			return true;
		},

		submitMoney(key){

			if(this.busy){
				return false;
			}
			var that = this;

			that.data.period = that.period.join('|');
			switch(key){
				case 1:
					//data.min_period = that.min_period
					//data.max_period = that.max_period
					//that.data.period = that.period.join('|');
					break;

				case 2:

					if(that.money=='1'){
						that.data.min_money = that.min_money
						that.data.max_money = that.max_money
						if(that.max_money=="" || that.min_money ==""){
							alert("你没有输入金额!")
							return false;
						}
					}else{
						that.data.money = 0
						that.data.min_money = 0
						that.data.max_money = 0
					}



					break;

				case 3:

					that.data.borrow_type = that.borrow_type
					break;

				case 5:

					that.data.retain_min_money = that.retain_min_money
					break;



			}

			this.$store.dispatch('setShowLoading', true);
			this.busy = true;
			this.$http.post(Api_Set_AutoBuy, that.data,'').then(function (response) {
				//window.history.go(-1);
				this.$store.dispatch('setShowLoading', false);
				this.busy = false;
				if(response.data.status){
					this.$route.router.go('/auto-buy')
				}else{
					alert(response.data.msg);
					if(response.data.code == '22221')
						this.$route.router.go('/login')
				}
			});
		},

		fetch(){
			var that = this;
			this.$http({url: Api_Get_AutoBuy, method: 'POST'}).then(function (response) {

				var obj = response.data;
				if(!obj.status){
					alert(obj.msg)
					if(obj.code == '10055'){
						sessionStorage.removeItem("login");
						this.$route.router.go('/login?from='+this.$route.path.substring(1))
					}
					return;
				}



				var rs = response.data, status = response.data.status;
				if(status){
					that.max_money = rs.data.max_money
					that.min_money = rs.data.min_money
					that.min_period = rs.data.min_period
					that.max_period = rs.data.max_period
					that.borrow_type = rs.data.borrow_type
					that.retain_min_money = rs.data.retain_min_money == '0.00' ? '0':rs.data.retain_min_money;


					//if(that.typeId == "2") that.cview = 1;
					//if(that.typeId == "3" && that.min_money!="" && that.max_money!="") that.cview = 1;

					that.period = rs.data.period || "";
					that.period = that.period.split(',');


					that.money = rs.data.money;

					that.data = rs.data;
				}

			});

		}
	},
	route:{
		activate( transition ){
			this.typeId = transition.to.params.typeId

			var that = this;

			switch (that.typeId) {
				case '1':
					that.title_msg = '投标期限'
					break;
				case '2':
					that.title_msg = '投标期限'
					break;
				case '3':
					that.title_msg = '投标金额'
					break;
				case '4':
					that.title_msg = '投标项目'
					break;
				case '5':
					that.title_msg = '保留金额'
					break;
			}

			transition.next();
			this.fetch()
		}
	}
}
</script>
